import 'package:appflutterhomeschool/api/api_service.dart';
import 'package:appflutterhomeschool/api/dio_utils.dart';
import 'package:appflutterhomeschool/common/common.dart';
import 'package:appflutterhomeschool/route/application.dart';
import 'package:appflutterhomeschool/route/routes.dart';
import 'package:appflutterhomeschool/ui/dialogs/prompt_dialog_widgets.dart';
import 'package:appflutterhomeschool/utils/sp_util.dart';
import 'package:flutter/material.dart';

///用户：Nine Tailed Fox
///时间：2020/6/8
///邮箱：1037438704@qq.com
///功能：我的页面状态管理

class MyViewModel extends ChangeNotifier {
  //true 是老师  false 是家长
  bool identity;

  String imageUrl = Constant.nullImage;
  String nameTitle = "";

  String clsNum = "";

  String titleIdentity = "家长";

  String imageInformation;
  String titleInformation;

  String imageResourceBase;
  String titleResourceBase;
  BuildContext me;

  MyViewModel(this.me, {this.identity});

  //获取身份
  getIdentityApplication() {
    this.identity = identity;
    initView();
    initData();
  }

  initView() {
    identity = SpUtil.getString('role_now') == "TEACHER";
    if (identity) {
      titleIdentity = "教师";
      imageInformation = "geren_banji";
      titleInformation = "班级信息";
      imageResourceBase = "geren_ziyuan";
      titleResourceBase = "资源库";
    } else {
      titleIdentity = "家长";
      imageInformation = "geren_xuesheng";
      titleInformation = "学生信息";
      imageResourceBase = "geren_dignzhi";
      titleResourceBase = "定制学";
    }
    notifyListeners();
  }

  initData() {
    Map<String, dynamic> map = {"role": Constant.getRole()};
    DioUtils.getInstance(tag: 1).post(ApiService.userInfo, map,
        (successCallBack) {
      imageUrl = successCallBack["data"]["img"];
      SpUtil.putString("img", imageUrl);
      nameTitle = successCallBack["data"]["name"];
      if (successCallBack["data"]["cls_num"] == 0) {
        if (identity) {
          clsNum = "未加入班级";
        } else {
          clsNum = "";
        }
      } else {
        clsNum = "已加入${successCallBack["data"]["cls_num"]}个班级";
      }
      notifyListeners();
    }, (errorCallBack) {});
  }

  initEvent(int page) {
    switch (page) {
      case 0:
        //学生信息
        Application().navigatePushToPage(me, Routes.studentInformationPage, {});
        break;
      case 1:
        //定制学
        Application().navigatePushToPage(me, Routes.customizationPage, {});
        break;
      case 2:
        Application().navigatePushToPage(me, Routes.switchIdentityPage,
            {"pageTag": 1}).then((value) => initData());
        break;
      case 3:
        //客服
        Application().navigatePushToPage(me, Routes.customerServicePage, {});
        break;
      case 4:
        //设置
        Application().navigatePushToPage(me, Routes.setUpPage, {});
        break;
      case 5:
        //退出登录
        PromptDialogWidgets(me).showPromptDialog(
          cancelOnTap: () {
            //清除数据
            //清除学生班级数据
            SpUtil.remove('getStudent');
            SpUtil.remove("token");
            SpUtil.remove("role");
            Navigator.pop(me);
            Application().navigatePushToPage(me, Routes.loginPage, {});
          },
        );
        break;
      default:
        break;
    }
  }
}
